<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/12/17
 * Time: 16:08
 */
namespace app\api\controller;
use think\Db;
class Home extends Base
{
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            echo json_encode(['status'=>301,'msg'=>"Token失效！请重新登录！"]);
            exit;
        }
    }

    /*
     * 获取轮播图
     */
    public function getBanner(){
        extract(input());
        $data = Db::name('lunbo')
            ->where(['lunbo_type'=>$type])
            ->field(['lunbo_id','lunbo_image'])
            ->limit (5)
            ->select();
        return json(['status'=>200,'msg'=>$data]);
    }

    /*
     * 获取轮播图内容
     */
    public function getBannerContent(){
        extract(input());
        $data = Db::name('lunbo')
            ->where(['lunbo_id'=>$lunbo_id])
            ->field(['lunbo_id','lunbo_content'])
            ->find();
        return json(['status'=>200,'msg'=>$data]);
    }

    /*
     * 获取精品推荐和滚动文章
     */
    public function getPerfect(){
        $data = Db::name('goods g')
            ->join ('pinpai p','g.good_pinpai = p.p_id','left')
            ->where(['good_top'=>2])
            ->field(['good_id','good_name','good_thumb','p_name'])
            ->limit (3)
            ->select ();
        $scroll = Db::name('market')
            ->field(['mar_id','mar_title'])
            ->page(1,5)
            ->order('mar_create_time desc')
            ->select ();
        return json(['status'=>200,'msg'=>$data,'scroll'=>$scroll]);
    }

    /*
     * 获取热门商品
     */
    public function getHot(){
        extract(input());
        $data = Db::name('goods')
            ->where(['good_hot'=>2])
            ->field(['good_id','good_name','good_price','good_thumb','y_price'])
            ->page($page,10)
            ->order ('good_create_time desc')
            ->select ();
        return json(['status'=>200,'msg'=>$data]);
    }

    /*
     * 获取回收行情
     */
    public function getRecovery(){
        $data = Db::name('recovery')
            ->where(['rec_status'=>1])
            ->field('rec_id,rec_name,rec_money')
            ->order('rec_create_time desc')
            ->select ();
        return json(['status'=>200,'msg'=>$data]);
    }

    /*
     * 获取行情分类
     */
    public function getMarket(){
        $data = Db::name('mar_type')
            ->where(['mar_status'=>1])
            ->field('mar_id,mar_name')
            ->select ();
        return json(['status'=>200,'msg'=>$data]);
    }

    /*
     * 获取文章列表
     */
    public function getMarketList(){
        extract(input());
        $data = Db::name('market')
            ->where(['mar_type'=>$mar_type])
            ->field(['mar_id','mar_title','mar_num','mar_image',"DATE_FORMAT(mar_update_time,'%Y-%m-%d')"=>'mar_create_time'])
            ->page($page,10)
            ->order('mar_update_time desc')
            ->select ();
        return json(['status'=>200,'msg'=>$data]);
    }

    /*
     * 获取文章详情
     */
    public function getMarketContent(){
        extract(input());
        Db::name('market')
            ->where(['mar_id'=>$mar_id])
            ->setInc ('mar_num',1);
        $data = Db::name('market')
            ->where(['mar_id'=>$mar_id])
            ->field(['mar_id','mar_title','mar_num','mar_content'])
            ->find();
        return json(['status'=>200,'msg'=>$data]);
    }


    /*
     * 关于我们/联系我们
     */
    public function aboutUs(){
        $data = Db::name('about')
            ->where(['a_id'=>1])
            ->field(['a_logo','a_content','a_name','a_law','a_agree','a_phone','a_email','a_address'])
            ->find();
        return json(['status'=>200,'msg'=>$data]);
    }

    /*
     * 反馈建议
     */
    public function suggest(){
        extract(input());
        $param = [
            's_content'=>$content,
            's_user_id'=>$user_id,
            's_time'=>date('Y-m-d H:i:s')
        ];
        $res = Db::name('suggest')->insert($param);
        if($res){
            return json(['status'=>200,'msg'=>'反馈成功']);
        }else{
            return json(['status'=>100,'msg'=>'反馈失败']);
        }
    }
}